import * as pdfjs from 'pdfjs-dist'

// pdfjs.GlobalWorkerOptions.workerSrc = "pdfjs-dist/build/pdf.worker.js"
import * as pdfjsWorker from 'pdfjs-dist/build/pdf.worker.js'
window.pdfjsWorker = pdfjsWorker


async function convertPdfToImage(pdfUrl) {
  const pdf = await pdfjs.getDocument(pdfUrl).promise;
  const canvas = document.createElement('canvas');
  const context = canvas.getContext('2d');
  let imgArr = []
  for (let i = 1; i <= pdf._pdfInfo.numPages; i++) {
    const page = await pdf.getPage(i);
    const viewport = page.getViewport({ scale: 1 });
    canvas.width = viewport.width;
    canvas.height = viewport.height;
    await page.render({ canvasContext: context, viewport: viewport }).promise;
    imgArr.push(canvas.toDataURL('image/png'))
  }
  return imgArr
}

export default  convertPdfToImage